const SIZE_UNIT = ["B", "KB", "MB", "GB", "TB"];
const DEFAULT_ORDER_BIT = 3;

const typeChecking = (objTypeArr=[]) => {
    // 支持多个数据的类型检查
    objTypeArr.forEach((item, i) => {
        if ((typeof (item[0]) !== item[1])) {
            console.error("==>> item", item)
            return false;
            // if (!(item[0] instanceof item[1])) {
            //     console.error("数据类型不符！")
            //     return false;
            // }
        }
    })
    
    return true;
}

const sizeHandler = (size, unit = 0, decimal = 2) => {
    
    if (size < 1024) {
        return [size.toFixed(decimal), SIZE_UNIT[unit], unit];
    }
    let _size = size / 1024;
    let _unit = unit + 1;
    return sizeHandler(_size, _unit, decimal)
}

const sizeFormat = (size = 0, unit = 0, decimal=1) => {

    if (typeChecking([[size, "number"], [unit, "number"], [decimal, "number"],])) {
        return sizeHandler(size, unit, decimal);
    }
    else {
        return ["数据类型错误!", ""];
    }
}

const stringToDate = (str = '') => {  // 此处知识处理成我想要的格式，并没有通用化处理。

    if (typeChecking([[str,"string"], ])) {
        const _date = new Date(str);
        const year = `${_date.getFullYear()}`.padStart(4, "0");
        const month = `${_date.getMonth() + 1}`.padStart(2, "0");
        const date = `${_date.getDate()}`.padStart(2, "0");
        const hour = `${_date.getHours()}`.padStart(2, "0");
        const minute = `${_date.getMinutes()}`.padStart(2, "0");
        return `${year}-${month}-${date} ${hour}:${minute}`
    }
    else {
        return "数据类型错误!";
    }
}

// 排序参考：https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
const nameSort = (item, next) => {

    if (item.isDir == next.isDir) {

        return item.name > next.name;
    }
    else {
        if (item.isDir) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

const sizeSort = (item, next) => {

    if (item.isDir == next.isDir) {

        if (item.size > next.size) {
            return 1;
        }
        else {
            return -1;
        }
    }
    else {
        if (item.isDir) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

const dateSort = (item, next) => {

    if (item.isDir == next.isDir) {

        if (Date.parse(item.modified) > Date.parse(next.modified)) {
            return 1;
        }
        else {
            return -1;
        }
    }
    else {

        if (item.isDir) {
            return -1;
        }
        else {
            return 1;
        }
    }
}

const dateNow = (separator='-') => {

    const date_now = new Date();
    const year = `${date_now.getFullYear()}`.padStart(4, "0");
    const month = `${date_now.getMonth() + 1}`.padStart(2, "0");
    const date = `${date_now.getDate()}`.padStart(2, "0");
    return [year, month, date].join(separator)
}

const getOrderBit = (dataNum) => {
    // 确定序号的长度
    if (dataNum < 1) {
        return false;
    }
    let temp = dataNum / 10;
    let bit = 1;
    while (temp > 1) {
        bit ++;
        temp /= 10;
    }
    bit > DEFAULT_ORDER_BIT ? bit : bit = DEFAULT_ORDER_BIT;

    return bit;
}

function* batchRes(request, payload, call) {
    let batchResponse = new Response({
        ok: true,
        status: 200
    });
    for (const item of payload.rows) {
        const args = {
            id: item.id || item.name || item.hash,
            patch: payload.value
        };
        const response = yield call(request, args)
        if (!response.ok) {
            batchResponse = response;
            break;
        }
    }
    return batchResponse;
}

export {
    typeChecking,
    sizeFormat,
    stringToDate,
    nameSort,
    sizeSort,
    dateSort,
    dateNow,
    getOrderBit,
    batchRes,
};

